package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.Map;

import static Driver.DriverSingleton.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

//Visitor Page 封装了访客搜索、禁用和启用以及确定/取消的操作。

public class VisitorPage extends BasePage{
    WebDriver driver = getChromeDriver();
    Map<String,Object> vars = getVars();

    public VisitorPage goVisitorPage(){
        super.login();
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(4)")).click();
        waitForWindow(2000);
        assertThat(driver.getTitle(), is("访客管理"));
        return this;
    }

    //获取表格中访客姓名的列表
    public List<WebElement> getVisitor(){
        waitForWindow(2000);
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[2]/div[1]/div[3]/table/tbody/tr/td[1]/div/div/span[2]"));
        return elements;
    }

    //按照名字搜索访客
    public String getSearchRes(String keys){
        clickInput().search(keys).enter();
        waitForWindow(2000);
        List<WebElement> elements = getVisitor();
        //如果搜索结果为空，则返回null
        if(elements.size() == 0) return "null";
        for(WebElement element: elements){
            System.out.println(element.getText());
            assertThat( element.getText(), containsString(keys) );
        }
        //搜索并检测成功则返回success
        return "success";
    }

    //点击搜索框
    public VisitorPage clickInput(){
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).click();
        return this;
    }
    //模糊搜索
    public VisitorPage search(String keys){
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).sendKeys(keys);
        return this;
    }

    //回车
    public VisitorPage enter(){
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).sendKeys(Keys.ENTER);
        return this;
    }

    //禁用
    public VisitorPage disable(){
        //driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button")).click();
        driver.findElement(By.xpath("//span[contains(.,'禁用')]")).click();
        return this;
    }

    //启用
    public VisitorPage enable(){
        //driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button")).click();
        driver.findElement(By.xpath("//span[contains(.,'启用')]")).click();
        return this;
    }

    //确定
    public VisitorPage confirm(){
        driver.findElement(By.xpath("//button[contains(.,'确定')]")).click();
        return this;
    }

    //取消
    public VisitorPage cancel(){
        driver.findElement(By.xpath("//button[contains(.,'取消')]")).click();
        return this;
    }

    public String getState(){
        waitForWindow(2000);
        WebElement element =  driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[2]/div[1]/div[3]/table/tbody/tr[1]/td[9]"));
        return element.getText();
    }


}
